<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
    <meta content=always name=referrer>
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/css/newbase.css">
    <script src="__STATIC__/js/jquery-1.8.2.js"></script>
    <title>我的收藏</title>

    {include file="public/href" /}
</head>
<body>
<div class="wrap-sc">
    <header>
        <ul>
            <li {eq name=":input('type',1)" value='1'}class="cur"{/eq}><a href="?type=1">商品</a></li>
            <li {eq name=":input('type',1)" value='2'}class="cur"{/eq}><a href="?type=2">店铺</a></li>
            <input type="hidden" id="type"  value="{:input('type',1)}"  />
        </ul>
        <img src="__STATIC__/img_user/back.png" class="back" onclick=window.location.href="{:url('mobile/user_info/my_shop_index')}">
        <span><a href="javascript:change_display();" id="edit">编辑</a></span>
    </header>
    <div class="content">
        <div class="cur" id="goods"></div>
        <div id="shop" style="display: block;"></div>
    </div>
    <div class="footer" style="display: none;">
        <div class="all">
            <div class="choose" id="zong"><a href="javascript:void(0);"><input type="checkbox" id="radio-1-3" name="radio-1-set" class="radio allCk"/><label for="radio-1-3" onclick="check_all()">全选</label> </a></div>
            <div class="total" onclick="del()">
                <span class="settle">   取消收藏   </span>
            </div>
        </div>
    </div>
    {include file='public/src' /}
    <script src="__STATIC__/js/layui/layui.js"></script>
    <script>
        //layer流加载
        layui.use('flow', function() {
            var $ = layui.jquery;
            var flow = layui.flow;
            if ($('#type').val() == 1){    //收藏商品
                flow.load({
                    elem: '#goods' //流加载容器
                    ,isAuto:true
                    ,isLazyimg:true
                    ,done: function(page, next){ //执行下一页的回调
                        var lis = [];
                        var type = $('#type').val();
                        if($('#edit').text() != '编辑'){
                            var cla = '';
                        }else{
                            var cla =   'style="display:none"';
                        }


                        $.ajax({
                            url:"{:url('mobile/user_info/my_collect')}",
                            dataType:'json',
                            type:'post',
                            data:{'page':page,'type':type},
                            success:function(t){
                                console.log(t);
                                layui.each(t.data, function(index, item){
                                    if (item.shop_goods) {
                                        lis.push('<div class="tc" id="show_'+item.id+'"><div class="choose" '+cla+'><a href="#"><label for="radio-1-2"></label>  <input type="checkbox" name="radio-1-set" class="radio sun" onclick="is_all_checked()" value="'+item.id+'" /></a></div>' +
                                            '<div class="tc1"><div class="center" style="padding:5px 3% 5px 100px;"><a href="javascript:jump_goods_details('+item.shop_goods.id+');"><img src="/'+item.shop_goods.thumb_img+'" onerror=this.src="/static/mobile/img/empty_img.png"><div>' +
                                            '<h4>'+item.shop_goods.title+'</h4><div class="price" style="float: left;">' +
                                            '<span style="margin-top: 30px;">&yen;'+item.shop_goods.price+'</span></div>'+
                                            '<span style="float:right;margin-top: 30px;font-size: 12px;">销量'+item.shop_goods.sale_number+'</span></div></a></div></div></div></div>');
                                    }

                                });
                                next(lis.join(''), page < t.pages);
                            },
                            error: function () {
                                layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
                            }
                        })
                    }
                });
            }else{   //收藏店铺
                flow.load({
                    elem: '#shop' //流加载容器
                    ,isAuto:false
                    ,isLazyimg:true
                    ,done: function(page, next){ //执行下一页的回调
                        var lis = [];
                        var type = $('#type').val();
                        if($('#edit').text() != '编辑'){
                            var cla = '';
                        }else{
                            var cla =   'style="display:none"';
                        }
                        $.ajax({
                            url:"{:url('mobile/user_info/my_collect')}",
                            dataType:'json',
                            type:'post',
                            data:{'page':page,'type':type},
                            success:function(t){

                                layui.each(t.data, function(index, item){
                                    if (item.shop==null) {
                                        return
                                    }
                                    lis.push('<div class="tc" id="show_'+item.id+'"><div class="choose"'+cla+'><a href="#"><label for="radio-1-4"></label><input type="checkbox" name="radio-1-set" class="radio sun" value="'+item.id+'" onclick="is_all_checked()" /></a>' +
                                        '</div><div class="tc1"><div class="main"><a href="javascript:jump_shop('+item.collect_id+');" style="display: block;overflow: hidden;"><div class="left"><img style="border-radius: 100%" src="/'+item.shop.logo+'" onerror=this.src="/static/mobile/img/empty_img.png" ></div>' +
                                        '<div class="right"><div>'+item.shop.title+'</div>' +
                                        '<div>'+item.shop.collect_number+'人收藏</div></div></a></div></div></div>');
                                });
                                next(lis.join(''), page < t.pages);
                            },
                            error: function () {
                                layer.open({content:'网络繁忙,请重试',skin:'msg',time:2});
                            }
                        })
                    }
                });
            }
        });
        function change_display(){
            $('.choose').not('#zong').toggle();
            $('.footer').toggle();
            var text = $('#edit');
            if (text.text() == '编辑'){
                text.text('完成');
            }else{
                text.text('编辑');
                attr('sty');
            }
        }
        //全选
        function check_all(){
            //console.log($('.allCk').prop('checked'));
            $('.sun').each(function (i,o) {
                $(o).prop('checked',!$('.allCk').prop('checked'));
            })
        }
        //检测是否全选
        function is_all_checked(){
            var zhi = 0;
            $('.sun').each(function (i,o) {
                if ($(o).prop('checked')){
                    zhi++;
                }
            });

            if (zhi == $('.sun').length){
                $('.allCk').prop('checked',true);
            }else{
                $('.allCk').prop('checked',false);
            }
        }
        //删除收藏
        function del() {
            var str = '';
            $('.sun:checked').each(function (i,o) {
                if (str == ''){
                    str = $(o).val();
                }else {
                    str += ','+$(o).val();
                }
            });
            console.log(str);
            if (str == ''){
                layer.open({content:'请选择删除项',skin:'msg',time:2});
            }else{
                layer.open({
                    content: '你要取消收藏店铺吗？'
                    ,btn: ['确定', '取消']
                    ,yes: function(index){
                        var arr = str.split(",");
                        $.post("del_collect",{'id':str,'type':"{:input('type')}"},function (t) {
                            if (t.code == 200){
                                layer.open({content:t.message,skin:'msg',time:2});
                                for (var i=0;i<arr.length;i++){
                                    $('#show_'+arr[i]).remove();
                                }
                            }else{
                                layer.open({content:t.message,skin:'msg',time:2});
                            }
                        });
                    }
                });

            }
        }
        //跳转到店铺
        function jump_shop(id) {
            var url = "{:url('mobile/user_category/shop_index')}?shop_id="+id;
            //sessionStorage.setItem('shop_back_url',window.location.href);
            location.href = url;
        }
        //跳转到商品详情
        function jump_goods_details(id) {
            var url = "{:url('mobile/user_category/goods_details')}?goods_id="+id;
            sessionStorage.setItem('goods_url_back',window.location.href);
            window.location.href = url;
        }
    </script>
</body>
</html>